//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package events_subscribe

import (
	"fmt"
	"strings"

	"atomgit.com/openlinksaas/proto-gen-go.git/events/events_idea"
	"atomgit.com/openlinksaas/proto-gen-go.git/events_api"
	"atomgit.com/openlinksaas/proto-gen-go.git/project_idea_api"
)

func processIdeaEvent(event *events_api.Event, cfgItemList []*CfgItem) (bool, error) {
	var err error
	var msg string

	{
		clearGroupEvent := &events_idea.ClearGroupEvent{}
		if event.EventData.MessageIs(clearGroupEvent) {
			err = event.EventData.UnmarshalTo(clearGroupEvent)
			if err != nil {
				return false, err
			}
			msg = fmt.Sprintf("%s 在项目 %s 清空知识点分组 %s。", event.UserDisplayName, event.ProjectName,
				clearGroupEvent.Title)
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.IdeaEvCfg.ClearGroup {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}

	{
		createIdeaEvent := &events_idea.CreateIdeaEvent{}
		if event.EventData.MessageIs(createIdeaEvent) {
			err = event.EventData.UnmarshalTo(createIdeaEvent)
			if err != nil {
				return false, err
			}
			msg = fmt.Sprintf("%s 在项目 %s 创建知识点 %s (关键词 %s)。", event.UserDisplayName, event.ProjectName,
				createIdeaEvent.Title, strings.Join(createIdeaEvent.KeywordList, " "))
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.IdeaEvCfg.CreateIdea {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}

	{
		updateIdeaContentEvent := &events_idea.UpdateIdeaContentEvent{}
		if event.EventData.MessageIs(updateIdeaContentEvent) {
			err = event.EventData.UnmarshalTo(updateIdeaContentEvent)
			if err != nil {
				return false, err
			}
			msg = fmt.Sprintf("%s 在项目 %s 更新知识点 %s 内容 (原标题 %s)。", event.UserDisplayName, event.ProjectName, updateIdeaContentEvent.NewTitle, updateIdeaContentEvent.OldTitle)
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.IdeaEvCfg.UpdateIdeaContent {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}

	{
		updateIdeaKeywordEvent := &events_idea.UpdateIdeaKeywordEvent{}
		if event.EventData.MessageIs(updateIdeaKeywordEvent) {
			err = event.EventData.UnmarshalTo(updateIdeaKeywordEvent)
			if err != nil {
				return false, err
			}
			msg = fmt.Sprintf("%s 在项目 %s 更新知识点 %s 关键词(新关键词 %s,原关键词 %s)。", event.UserDisplayName, event.ProjectName,
				updateIdeaKeywordEvent.Title, strings.Join(updateIdeaKeywordEvent.NewKeywordList, " "), strings.Join(updateIdeaKeywordEvent.OldKeywordList, " "))
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.IdeaEvCfg.UpdateIdeaKeyword {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}

	{
		removeIdeaEvent := &events_idea.RemoveIdeaEvent{}
		if event.EventData.MessageIs(removeIdeaEvent) {
			err = event.EventData.UnmarshalTo(removeIdeaEvent)
			if err != nil {
				return false, err
			}
			msg = fmt.Sprintf("%s 在项目 %s 把知识点 %s 移入回收站。", event.UserDisplayName, event.ProjectName, removeIdeaEvent.Title)
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.IdeaEvCfg.RemoveIdea {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}

	{
		setAppraiseEvent := &events_idea.SetAppraiseEvent{}
		if event.EventData.MessageIs(setAppraiseEvent) {
			err = event.EventData.UnmarshalTo(setAppraiseEvent)
			if err != nil {
				return false, err
			}
			appraiseText := "赞同"
			if setAppraiseEvent.AppriaseType == uint32(project_idea_api.APPRAISE_TYPE_APPRAISE_DIS_AGREE) {
				appraiseText = "不赞同"
			}
			msg = fmt.Sprintf("%s 在项目 %s %s 知识点 %s。", event.UserDisplayName, event.ProjectName, appraiseText, setAppraiseEvent.Title)
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.IdeaEvCfg.SetAppraise {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}

	{
		cancelAppraiseEvent := &events_idea.CancelAppraiseEvent{}
		if event.EventData.MessageIs(cancelAppraiseEvent) {
			err = event.EventData.UnmarshalTo(cancelAppraiseEvent)
			if err != nil {
				return false, err
			}
			msg = fmt.Sprintf("%s 在项目 %s 取消标记 知识点 %s。", event.UserDisplayName, event.ProjectName, cancelAppraiseEvent.Title)
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.IdeaEvCfg.CancelAppraise {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}

	{
		importIdeaEvent := &events_idea.ImportIdeaEvent{}
		if event.EventData.MessageIs(importIdeaEvent) {
			err = event.EventData.UnmarshalTo(importIdeaEvent)
			if err != nil {
				return false, err
			}
			msg = fmt.Sprintf("%s 在项目 %s 导入知识点仓库 %s(导入%d条)。", event.UserDisplayName, event.ProjectName, importIdeaEvent.Name, importIdeaEvent.Count)
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.IdeaEvCfg.ImportIdea {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}

	return false, nil
}
